﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MDI
{
    public partial class ChildOne : Form
    {
        //This project is to show to pass data between forms after the forms are open.
        //I used the typical MDI parent and child scenarion where somthing is happening in the
        //child form and you need to display a message or invoke a method in the
        //parent form.

        //Read more about passing data between forms in the 
        //article "Passing Data Between Forms" by Thiagarajan Alagarsamy in The Code Project"
        //http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx

        private MDI mdiParent;

        #region Properties...

        private string messageFromParent;
        public string MessageFromParent 
		{
			get { return messageFromParent; }
            set { messageFromParent = value; }
		}

        #endregion


        #region Initialize...

        public ChildOne()
        {
            InitializeComponent();
        }

        //override constructor with the parent form as a parameter
        public ChildOne(MDI m1_)
        {
            InitializeComponent();
            this.mdiParent = m1_;
        }

        //call the method to display the value send by the 
        //parent form to the child's property
        private void ChildOne_Load(object sender, EventArgs e)
        {
            DisplayMessageFromParent();

        }

        #endregion


        #region Methods and functions...

        //this step is not really necassary, but I like to use properties to store the data.
        //You could have in the ChildOne_Load event used:
        //lblMessageFromParent.Text = MessageFromParent;
        //directly
        public void DisplayMessageFromParent()
        {
            lblMessageFromParent.Text = MessageFromParent;

        }

        #endregion


        #region Click Events...

        private void btnPassMessageToParent_Click(object sender, EventArgs e)
        {
            this.mdiParent.ChildMessage = txtMessageToParent.Text;
            //this.m1.UpdateMessage(txtMessageToParent.Text);
            this.mdiParent.UpdateMessage();
            this.Close();

        }

        #endregion

    }
}
